<?php

class NewslettersController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Newsletters';
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'Newsletter.newsletter_date' => 'desc'
            ));
  }

  //public version
  function index() {
    $this->layout = "main";
    $this->set('title_for_layout','Newsletters for the Champaign Illinois Kennel Club');
    $this->set("meta_keywords","Newsletters Resourses Downloads Champaign Illinois Kennel Club, Central Illinois, Champaign");
    $this->set("meta_description","Newsletter archive for the Champaign Illinois Kennel Club");
    $this->set('newsletters', $this->paginate('Newsletter'));
  }

//admin version
  function member_index() {
    $this->set('newsletters', $this->paginate('Newsletter'));
  }

  //admin
  function member_add() {
    $test = $this->Newsletter->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  //admin
  function member_edit($id = null) {
    $this->Newsletter->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Newsletter->read();
      $this->request->data['Newsletter']['newsletter_date'] = $this->Util->formatDate($this->request->data['Newsletter']['newsletter_date'], true);
    } else {
      //check for delete
      if (isset($this->request->data['deleteBtn'])) {
        $this->member_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Newsletter->set($data);
      if ($this->Newsletter->validates()) {
        $data["Newsletter"]["newsletter_date"] = $this->Util->formatDate($data["Newsletter"]["newsletter_date"]);
        if (!empty($data["Newsletter"]["file_path"]) && !empty($data["Newsletter"]["file_path"]['name'])) {
          $fileUploaded = $this->Upload->uploadFile($data["Newsletter"]["file_path"], WWW_ROOT . '/'.Configure::read('App.documentsFolder').'/' .Configure::read('App.newslettersFolder') );
          $data["Newsletter"]["file_path"] =Configure::read('App.documentsFolder').'/' . Configure::read('App.newslettersFolder') . '/' . $fileUploaded;
        } else {
          if($action==='add') {
            $data["Newsletter"]["file_path"] = null;
          } else {
            $data["Newsletter"]["file_path"] = isset($data["Newsletter"]["_file_path"]) ? $data["Newsletter"]["_file_path"] : null;
          }
        }
        if ($this->Newsletter->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          //getting previous image
          $data['Newsletter']['file_path']=$data["Newsletter"]["_file_path"];
          $data['Newsletter']['id']=$id;
        }
        $this->request->data['Newsletter'] = $data['Newsletter'];
        $this->set('errors', $this->Newsletter->validationErrors);
      }
      if($action==='add') {
        $this->render('member_add');
      } else {
        $this->render('member_edit');
      }
  }

  //admin
  function member_delete($id) {
    $this->member_deleteFile($id, false);
    if ($this->Newsletter->delete($id)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  //admin
  function member_deleteFile($id, $redirect=true) {
    $file = $this->Newsletter->read('file_path', $id);
    $filePath = $file['Newsletter']['file_path'];
    if (!empty($filePath)) {
      $result = $this->Util->deleteFile(WWW_ROOT . '/' . $filePath);
      $this->Newsletter->save(array('Newsletter' => array('id' => $id, 'file_path' => '')), false, array('file_path'));
      if($redirect){
        $this->Session->setFlash('The file has been deleted.');
        $this->redirect(array('action' => 'edit', $id));
      }
    }
  }

}

?>
